<?php
namespace My\Controller;

use Utuo\BackgroundController;

class WechatActivityInviteInviterController extends BackgroundController
{
    // aid
    private $inviteId;
    // session prifix
    const SESSION_KEY = 'WECHAT_ACTIVITY_INVITE_INVITER_ID';
    // init
    public function _initialize()
    {
        $this->setControllerTitle('邀请人管理');
        $this->setActionTitle(array(
            'index' => '邀请人列表',
            'add' => '添加邀请人',
            'recycle' => '回收站',
            'modify' => '修改邀请人',
            'delete' => '删除邀请人',
        ));
        
        $this->setActionNav('index,add,recycle');
        $inviteId = I('inviteId',0,'intval');
        $inviteId && session(self::SESSION_KEY, $inviteId);
        $this->inviteId = session(self::SESSION_KEY);
        $_POST['aid'] = $this->inviteId;
    }
    
    // getList
    public function getLists($is_deleted)
    {
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $d = D('WechatActivityInviteInviter a');
        $condition = array(
            'a.cid' => $this->aid,
            'a.aid' => $this->inviteId,
            'a.is_deleted' => $is_deleted
        );
        $list = $d->where($condition)
        	->field('a.*,(select count(b.id) from '.C('DB_PREFIX').'wechat_activity_invite_register b where b.inviterId=a.id) as icount')
            ->order('a.id DESC')
            ->select();
        foreach ($list as &$v) {
            $v['activityUrl'] = $this->getUrl($this->aid,$this->inviteId, $v['id']);
        }
        $this->assign('itemList', $list);
        $this->display();
    }
    

    // 邀请列表
    public function registerAction(){
        $this->setActionTitle('register','邀请人名单');
        $condition = array(
            'cid' => $this->aid,
            'inviterId' => I('id',0,'intval')
        );
        $lists = M("WechatActivityInviteRegister")->where($condition)->select();
        $this->assign('lists',$lists);
        $this->display();
    }
    
    // 获取前台活动URL
    protected function getUrl($cid, $aid,$inviterId)
    {
        $url = U('Home/Invite/index', array(
            'cid' => $cid,
            'aid' => $aid,
            'inviterId' => $inviterId
        ), true, true);
        return $url;
    }
}